/*
 * Palette.java
 *
 * Created on 26 maj 2007, 23:43
 *
 */

package pl.krakow.ae.knp.wikiboard.gui.palette;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import pl.krakow.ae.knp.wikiboard.gui.board.Board;
import pl.krakow.ae.knp.wikiboard.gui.bundles.Lang;
import pl.krakow.ae.knp.wikiboard.gui.tools.ToolType;
import pl.krakow.ae.knp.wikiboard.utils.GraphicalUtils;

/**
 *
 * @author m
 */
public class Palette extends JPanel {
    private static final int BUTTON_SIZE = 30;
    
    private static final String drawStraightLineIconPath = "images/buttons/btDrawStraightLine.gif";
    private static final String drawLineIconPath = "images/buttons/btDrawLine.gif";
    private static final String drawTextIconPath = "images/buttons/btDrawText.gif";
    private static final String drawEllipseIconPath = "images/buttons/btDrawEllipse.gif";
    private static final String drawRectangleIconPath = "images/buttons/btDrawRectangle.gif";
    private static final String fillIconPath = "images/buttons/btFill.gif";
    private static final String selectIconPath = "images/buttons/btSelect.gif";
    private static final String deleteIconPath = "images/buttons/btDelete.gif";
    private static final String rotateIconPath = "images/buttons/btRotate.gif";
    private static final String resizeIconPath = "images/buttons/btResize.gif";
    private static final String undoIconPath = "images/buttons/btUndo.gif";
    private static final String redoIconPath = "images/buttons/btRedo.gif";
    
    private static final String WARNING_MESSAGE = Lang.get("are_you_sure_clean");
    
    private JToggleButton
            btDrawStraightLine,
            btDrawLine,
            btDrawText,
            btDrawEllipse,
            btDrawRectangle,
            btFill,
            btSelect,
            btRotate,
            btResize,
            btDelete;
    private JButton btUndo, btRedo, btClearAll;
    private JButton btBgColor;
    
    private ConnectionPalette connectionPalette;
    
    private javax.swing.JButton btFgColor;

    private OtherPaletteEvent ope;
    
    private ButtonGroup group;
    
    private PaletteEventListener listener;
    
    /** Creates a new instance of Palette */
    public Palette(PaletteEventListener listener) {
        this.listener = listener;
        addComponents();
        layoutComponents();
    }
    
    private void addComponents() {
        btDrawStraightLine = new JToggleButton();
        btDrawStraightLine.setIcon(GraphicalUtils.createImageIcon(drawStraightLineIconPath));
        btDrawStraightLine.setToolTipText(Lang.get("draw_straight_line"));
        btDrawStraightLine.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        
        btDrawStraightLine.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new ToolChanged(ToolType.STRAIGHTLINE_MARKER)); }
        });
        
        btDrawLine = new JToggleButton();
        btDrawLine.setIcon(GraphicalUtils.createImageIcon(drawLineIconPath));
        btDrawLine.setToolTipText(Lang.get("draw_line"));
        btDrawLine.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        
        btDrawLine.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new ToolChanged(ToolType.LINE_MARKER)); }
        });
        
        btDrawText = new JToggleButton();
        btDrawText.setIcon(GraphicalUtils.createImageIcon(drawTextIconPath));
        btDrawText.setToolTipText(Lang.get("draw_text"));
        btDrawText.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        btDrawText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new ToolChanged(ToolType.TEXT_MARKER)); }
        });
        
        btDrawEllipse = new JToggleButton();
        btDrawEllipse.setIcon(GraphicalUtils.createImageIcon(drawEllipseIconPath));
        btDrawEllipse.setToolTipText(Lang.get("draw_ellipse"));
        btDrawEllipse.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        btDrawEllipse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new ToolChanged(ToolType.ELLIPSE_MARKER)); }
        });
        
        btDrawRectangle = new JToggleButton();
        btDrawRectangle.setIcon(GraphicalUtils.createImageIcon(drawRectangleIconPath));
        btDrawRectangle.setToolTipText(Lang.get("draw_rectangle"));
        btDrawRectangle.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        btDrawRectangle.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new ToolChanged(ToolType.RECT_MARKER)); }
        });
        
        btFill = new JToggleButton();
        btFill.setIcon(GraphicalUtils.createImageIcon(fillIconPath));
        btFill.setToolTipText(Lang.get("fill"));
        btFill.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        btFill.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new ToolChanged(ToolType.FILL_MARKER)); }
        });
        
        btSelect = new JToggleButton();
        btSelect.setToolTipText(Lang.get("select"));
        btSelect.setIcon(GraphicalUtils.createImageIcon(selectIconPath));
        btSelect.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        btSelect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new ToolChanged(ToolType.SELECTOR)); }
        });
        
        btDelete = new JToggleButton();
        btDelete.setIcon(GraphicalUtils.createImageIcon(deleteIconPath));
        btDelete.setToolTipText(Lang.get("delete"));
        btDelete.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        btDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new ToolChanged(ToolType.RUBBER)); }
        });
        
        btRotate = new JToggleButton();
        btRotate.setIcon(GraphicalUtils.createImageIcon(rotateIconPath));
        btRotate.setToolTipText(Lang.get("rotate"));
        btRotate.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        
        btResize = new JToggleButton();
        btResize.setIcon(GraphicalUtils.createImageIcon(resizeIconPath));
        btResize.setToolTipText(Lang.get("resize"));
        btResize.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        
        btUndo = new JButton();
        btUndo.setIcon(GraphicalUtils.createImageIcon(undoIconPath));
        btUndo.setToolTipText(Lang.get("undo"));
        btUndo.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        btUndo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new OtherPaletteEvent(OtherPaletteEventType.UNDO)); }
        });
        
        btRedo = new JButton();
        btRedo.setIcon(GraphicalUtils.createImageIcon(redoIconPath));
        btRedo.setToolTipText(Lang.get("redo"));
        btRedo.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        btRedo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                processPaletteEvent(new OtherPaletteEvent(OtherPaletteEventType.REDO)); }
        });
        
        btClearAll = new JButton(Lang.get("clean"));
        btClearAll.setPreferredSize(new Dimension(90, 30));
        btClearAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                clearBoard(); }
        });
        
        btBgColor = new JButton();
        btBgColor.setPreferredSize(new Dimension(20, 20));
        btBgColor.setBackground(Board.getBgColor());
        btBgColor.setOpaque(true);
        btBgColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                changeBgColor(); }
        });
        
        btFgColor = new JButton();
        btFgColor.setPreferredSize(new Dimension(20, 20));
        btFgColor.setBackground(Board.getFgColor());
        btFgColor.setOpaque(true);
        btFgColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                changeFgColor(); }
        });
        
        connectionPalette = new ConnectionPalette(listener);
    }
    
    private void layoutComponents() {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        
        c.insets = new Insets(1,1,1,1);
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        
        add(btDrawStraightLine, c);
        
        c.gridx = 1;
        add(btDrawLine, c);
        
        c.gridx = 2;
        add(btDrawText, c);
        
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(1, 1, 10, 1);
        add(btDrawEllipse, c);
        
        c.gridx = 1;
        add(btDrawRectangle, c);
        
        c.gridx = 2;
        add(btFill, c);
        
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(1, 1, 1, 1);
        add(btSelect, c);
        
        c.gridx = 1;
        add(btDelete, c);
        
        c.gridx = 2;
        add(btRotate, c);
        
        c.gridx = 0;
        c.gridy = 3;
        c.insets = new Insets(1, 1, 10, 1);
        add(btResize, c);
        
        c.gridx = 1;
        add(btUndo, c);
        
        c.gridx = 2;
        add(btRedo, c);
        
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 3;
        c.anchor = GridBagConstraints.NORTH;
        add(btClearAll, c);
        
        addButtonGroup();
        
        c.gridwidth = 1;
        c.gridy = 5;
        add(btBgColor, c);
        c.gridx = 1;
        add(btFgColor, c);
        
        c.gridx = 0;
        c.gridwidth = 4;
        c.anchor = GridBagConstraints.NORTH;
        c.gridy = 6;
        add(connectionPalette, c);
    }
    
    private void addButtonGroup() {
        group = new ButtonGroup();
        
        Component[] components = getComponents();
        for (Component elem : components) {
            if (elem instanceof AbstractButton)
                group.add((AbstractButton)elem);
        }
    }
    
    private void changeBgColor() {
        Color c = JColorChooser.showDialog(this, Lang.get("choose_bg_color"), Board.getBgColor());
        if (c != null) {
            btBgColor.setBackground(c);
            processPaletteEvent(new BgColorChanged(c));
        }
    }
    
    private void changeFgColor() {
        Color c = JColorChooser.showDialog(this, Lang.get("choose_fg_color"), Board.getFgColor());
        if (c != null) {
            btFgColor.setBackground(c);
            processPaletteEvent(new FgColorChanged(c));
        }
    }
    
    private void clearBoard() {
        int ret = JOptionPane.showConfirmDialog(this,
                WARNING_MESSAGE, "Wikiboard",
                JOptionPane.YES_NO_OPTION);
        if (ret == JOptionPane.OK_OPTION)
            processPaletteEvent(new OtherPaletteEvent(OtherPaletteEventType.CLEAR));
    }
    
    public void addPaletteEventListener(PaletteEventListener listener) {
        this.listener = listener;
    }
    
    public void processPaletteEvent(PaletteEvent event) {
        listener.paletteModified(event);
    }
    
    public String getTxAddress() {
        return connectionPalette.getTxAddress();
    }
}
